package com.example.neo4j.service;

import com.example.neo4j.model.Movie;
import com.example.neo4j.model.Person;
import com.example.neo4j.repository.MovieRepository;
import com.example.neo4j.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PersonService {
    @Autowired
    private PersonRepository personRepository;

    @Autowired
    private MovieRepository movieRepository;

    public Person createPerson(Person person) {
        return personRepository.save(person);
    }

    public Movie createMovie(Movie movie) {
        return movieRepository.save(movie);
    }

    public Person addMovieToPerson(String personId, String movieId) {
        Person person = personRepository.findById(personId).orElseThrow(() -> new RuntimeException("Person not found"));
        Movie movie = movieRepository.findById(movieId).orElseThrow(() -> new RuntimeException("Movie not found"));

        person.getMovies().add(movie);
        return personRepository.save(person);
    }

    public Iterable<Person> getAllPersons() {
        return personRepository.findAll();
    }

    public Iterable<Movie> getAllMovies() {
        return movieRepository.findAll();
    }
}
